<?php 
/** ***********************
 * 作者：卢逸 www.61php.com
 * 日期：2015/5/21
 * 作用：61php框架核心类
 ** ***********************/
class coreFrameworkController{
	function __construct(){
		global $GVar;
		$this->GVar=$GVar;
	}
	/*
	 * 实例化view
	 */
	function executeView(){
		global $path_view,$GVar;

		$className="{$GVar->fget['m']}View".ucfirst($GVar->fget['a']);

		if (!class_exists($className)){
			die(_lang_no_view);
		}
		return new $className();
	}
}
/*
 * 核心视图类
 */
class coreFrameworkView{
	
	function __construct()
	{
		global $GVar,$path_module,$Config;
		$this->GVar=$GVar;
		$this->Config=$Config;
		
		//实例化数据模型
		$path_model=$path_module."model.php";

		if (file_exists($path_model)) {
			require_once  $path_model;
			$className="{$this->GVar->fget['m']}Model".ucfirst($this->GVar->fget['a']);
			if (!class_exists($className)){
				die(_lang_no_model);
			}
			$this->model=new $className();
		}
		//载入模块语言包
		$language_file=$path_module."language".DS._DEFAULT_LANGUAGE.".php";
		if (file_exists($language_file)){
			require_once  $language_file;
		}
		
		//初始化模板引擎
		$this->tmp=new Smarty;
		$this->tmp->template_dir=_SITE_APPLICATION_TEMPLATE;
		
		$this->tmp->compile_dir =_SITE_CACHE."smarty".DS."compile".DS;
		$this->tmp->cache_dir =_SITE_CACHE."smarty".DS."cache".DS;
		$this->tmp->left_delimiter = '<{';
		$this->tmp->right_delimiter = '}>';

		//模板web路径
		$this->tmp->assign("template_dir",substr(_SITE_APPLICATION_TEMPLATE,strlen(_SITE_ROOT)));
		$this->tmp->assign("version",_lang_version);

		$this->tmp->assign("config",$this->Config);
	}

	function dp($filename){
		//输出页面
		$gvar["fpost"] = $this->GVar->fpost;
		$gvar["frequest"] = $this->GVar->frequest;
		$gvar["fget"] = $this->GVar->fget;
		$gvar["cookie"] = $this->GVar->cookie;
		$this->tmp->assign("gvar",$gvar);
		$template_name=$filename.".html";
		$this->tmp->display($template_name);
	}
	function assign($name,$val){
		$this->tmp->assign($name,$val);
	}
}
/*
 * 核心数据类
*/
class coreFrameworkModel{
	function __construct()
	{
		global $db,$Config;
		$this->db=$db;
		$this->config=$Config;
	}
	/**
	 * 获取全部数据
	 * @param unknown $sql  SQL语句
	 * @param string $database 数据源，默认数据源default
	 * @return string
	 */
	function GetAll($sql,$database=null){
		$sql=$this->setDataBase($database,$sql);
		return $this->db->GetAll($sql);
	}
	/**
	 * 获取指定字段
	 * @param unknown $sql  SQL语句
	 * @param string $database 数据源
	 * @return string
	 */
	function GetOne($sql,$database=null){
		$sql=$this->setDataBase($database,$sql);
		return $this->db->GetOne($sql);
	}
	/**
	 * 获取单条数据
	 * @param unknown $sql  SQL语句
	 * @param string $database 数据源，默认数据源default
	 * @return string
	 */
	function GetRow($sql,$database=null){
		$sql=$this->setDataBase($database,$sql);
		return $this->db->GetRow($sql);
	}
	/**
	 * 执行sql语句
	 * @param unknown $sql  SQL语句
	 * @param string $database 数据源，默认数据源default
	 * @return string
	 */
	function Execute($sql,$database=null){
		$sql=$this->setDataBase($database,$sql);
		return $this->db->Execute($sql);
	}
	/**
	 * 设置新的数据源
	 * @param string $databaseName 数据源名称
	 * @param string $sql  需要处理的sql语句
	 * @return string $sql
	 */
	function setDataBase($databaseName=null,$sql){
			global $DataBase;
			//如果原本就是这个直接停止
			if ($databaseName=="default" || !$databaseName){
				if ($this->db->database=="default") return $sql;
				$this->db->host=_DATABASE_HOST;
				$this->db->name=_DATABASE_USER;
				$this->db->pass=_DATABASE_PASSWORD;
				$this->db->table=_DATABASE_NAME;
				$this->db->ut=_DATABASE_UT;
			}else{
				$sql=str_replace(_TABLE_FIRST_NAME, $DataBase[$databaseName]['first_name'], $sql);
				if ($this->db->database==$databaseName) {
					return $sql;
				}
				$this->db->host=$DataBase[$databaseName]['host'];
				$this->db->name=$DataBase[$databaseName]['user'];
				$this->db->pass=$DataBase[$databaseName]['password'];
				$this->db->table=$DataBase[$databaseName]['name'];
				$this->db->ut=$DataBase[$databaseName]['ut'];
			}
			$this->db->database=($databaseName)?$databaseName:"default";
			$this->db->connect();
			return $sql;
	}
	/**
	 * 分页方法
	 * @param unknown $sql  
	 * @param unknown $get 
	 * @param string $cols
	 * @param string $use_ado 
	 * @param string $page_count_max
	 * @param string $page_class_file
	 * @param string $database
	 */
	function createPage($sql,$get,$cols="*",$use_ado=false,$page_count_max=null,$page_class_file="page.php",$database=null){
		$page_file=_SITE_INCLUDE_CLASS_PATH.$page_class_file;
		require $page_file;
		if (!$get['page']) $get['page']=1;
		if (!$cols) $cols="*";
		if (!$page_count_max) $page_count_max=$this->config['page_count_max'];
		$this->page_obj=new zPage($sql,$cols,$this->db,$use_ado,$page_count_max);
		$sql.=" limit ".$this->page_obj->this_lots_per_page*($get['page']-1).",".$this->page_obj->this_lots_per_page;
		return $this->GetAll($sql,$database);
	}
}

?>